<?php
$con=mysqli_connect($servername,$username,$password,$dbname);
if (mysqli_connect_errno()) {
	//数据库链接失败
    echo '数据库链接失败';
} else {
	//数据库链接成功
    $ip=$_SERVER["REMOTE_ADDR"];
    $result_log = mysqli_query($con,"SELECT * FROM star_php_log WHERE ip='$ip'");
    if ($result_log->num_rows == 0){//没有这个IP
        //insert就好了，我明天来
        $time=time();
        $sql = "INSERT INTO star_php_log (ip, time, times) VALUES ('$ip', '$time', '1')";
        mysqli_query($con, $sql);
    }else{
        while($row = mysqli_fetch_array($result_log)){
            if($row["black_times"]==5){
                exit("您被拦截");
            }elseif($row["black_time"]+$row["between"]>time()){
                exit("您被拦截");
            }elseif($row["time"]+10<time()){
                $time_now=time();
                $result_times = mysqli_query($con,"UPDATE `star_php_log` SET `times` = '1' WHERE `star_php_log`.`ip` = '$ip'");
                $result_time = mysqli_query($con,"UPDATE `star_php_log` SET `time` = '$time_now' WHERE `star_php_log`.`ip` = '$ip'");
            }else{
                if($row["times"]==11){//触发限制
                    $time_now=time();
                    header("Location:https://www.baidu.com/s?wd=%E6%94%BB%E5%87%BB%E7%BD%91%E7%AB%99%E8%BF%9D%E6%B3%95%E5%90%97");//跳转网页
                    mysqli_query($con,"UPDATE `star_php_log` SET `black_time` = '$time_now' WHERE `star_php_log`.`ip` = '$ip'");
                    mysqli_query($con,"UPDATE `star_php_log` SET `between` = '30' WHERE `star_php_log`.`ip` = '$ip'");
                    $black_times = $row["black_times"]+1;
                    mysqli_query($con,"UPDATE `star_php_log` SET `black_times` = '$black_times' WHERE `star_php_log`.`ip` = '$ip'");
                    if($black_times==5){
                        sendMail($smtpurl, $smtpemail, $smtpport, $smtpkey, $to,'蓝星云面板告警','被攻击提示','IP： '.$_SERVER["REMOTE_ADDR"].' 触发告警5次，已被永久封禁',$smtpserver,$smtpport,$from,$smtpkey);
                    }
                }else{
                    $times=$row["times"]+1;
                    $result_times = mysqli_query($con,"UPDATE `star_php_log` SET `times` = '$times' WHERE `star_php_log`.`ip` = '$ip'");
                }
            }
        }
    }
}
?>